#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Function Declaration */
void ex1();
void ex2();
void ex3();
void ex4();
void ex5();
void print_menu();
int perfect_num(int);

/* Main Function */
void main(){
	int key, cont =1;
	printf("Welcome to Exercise\n\n");
	print_menu();
	while (cont != 0){
		scanf("%d", &key);
		switch (key){
			case 1:{
				system("cls");
				ex1();
				print_menu();
				break;
				   }
			case 2:{
				system("cls");
				ex2();
				print_menu();
				break;
				   }
			case 3:{
				system("cls");
				ex3();
				print_menu();
				break;
				   }
			case 4: {
				system("cls");
				ex4();
				print_menu();
				break;
					}
			case 5:{
				system("cls");
				ex5();
				print_menu();
				break;
				   }
			case 0:{
				return;
				break;
				   }
			default:{

				cont = 0;
				break;
					}
		}
	}
}

/* Print Main menu */
void print_menu(){
	printf("Choose an exercise:\n\n");
	printf("1 - Advanced integer multiplier\n");
	printf("2 - Prime numbers\n");
	printf("3 - Reverse number\n");
	printf("4 - Fibonacci\n");
	printf("5 - Complex number\n");
	printf("0 - Exit\n");
}

/* Inteligent  Multiplier */ 
void ex1(){
	int num1, num2 = 0;
	printf("Insert the two positive numbers to multiply\n");
	scanf("%d%d", &num1, &num2);
	if(num1 < 0 || num2 < 0){
		printf("Unable to multiply negative numbers");
	}
	
	else if(num1 >= num2){
		int i, res = 0;
		for( i = (num2); i > 0; i--){
			res += num1;
		}
		printf("The result is: %d\n\n", res);
	}
	else{
		int i, res = 0;
		for( i = (num1); i > 0; i--){
			res += num2;
		}
		printf("The result is: %d\n\n", res);
	}

}
/* Inteligent prime number */
void ex2(){
 int i, sqroot, num = 50, prime_flag;
 sqroot = (int)sqrt(num);
 for (num = 50; num < 100; num ++){
	 prime_flag = 0;
	 for(i = 2; i < sqroot; i++){
		 if (num % i ==0){
			 prime_flag = 0;
			 break;
		 }
		 prime_flag = 1;
	 }
	 if (prime_flag ==1){
		 printf("Prime Number: %d\n", num);
	 }
 }
}
/* Reverse number function */
void ex3(){
	int num= 0, res = 0, temp = 0;
	printf("Insert an integer to reverse: \n");
	scanf("%d", &num);
	if(num < 0){
		printf("Unable to reverse negative numbers");
	}
	while (num > 0){
		temp = num % 10;
		num = num/10;
		res = 10*res + temp;
	}
	printf("The reverse number is: %d\n", res);
}
/* Fibonacci series counter */
void ex4(){
	int num, n0 = 0, n1 = 1, n2 = 0;
	printf("Insert the N number from Fibonacci series \n");
	scanf("%d", &num);
	if(num < 0){
		printf("Unable to get Fibonnaci negative numbers");
	}
	while (num != 0){
		n2 = n1 + n0;
		n0 = n1;
		n1 = n2;
		num --;
	}
	printf("The N number in Fibonacci series is: %d\n", n0);
}
/* Perfect number */
void ex5(){
	int i, res;
	for (i = 0; i < 1000; i ++){
		res = perfect_num(i);
		if(res !=0){
			printf("%d, is a perfect number\n", res);
		}
	}
	return;
}
/* perfect_number function */
int perfect_num(int num){
	int i, perf = 0;
	for (i = 1; i < num; i++){
		if (num % i == 0){
			perf += i;
		}
	}
	if (perf == num){
		return num;
	}
	return 0;
}